package com.zs.games.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zs.games.resultTool.ResultCodeEnum;
import com.zs.games.entity.UserComment;
import com.zs.games.entity.UserPraise;
import com.zs.games.enums.SourceTypeEnum;
import com.zs.games.exception.APIException;
import com.zs.games.mapper.UserCommentMapper;
import com.zs.games.mapper.UserPraiseMapper;
import com.zs.games.param.UserCommentParam;
import com.zs.games.service.CommentAndPraiseService;
import com.zs.games.utils.JwtTokenUtils;
import com.zs.games.vo.LoginUserVo;
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @date 2024年03月15日 18:19
 */
@Server
public class CommentAndPraiseServiceImpl implements CommentAndPraiseService {
    @Autowired
    private UserCommentMapper UserCommentMapper;
    @Autowired
    private UserPraiseMapper userPraiseMapper;
    @Override
    public Boolean praise(Integer sourceId, Integer sourceType) {
        // 实现点赞逻辑
        UserPraise userNewPraise = userPraiseMapper.selectOne(new QueryWrapper<UserPraise>().lambda()
                .eq(UserPraise::getSourceId, sourceId)
                .eq(UserPraise::getSourceType, sourceType)
                .eq(UserPraise::getUserId, JwtTokenUtils.getUserInfoA().getUserId()));
        if (userNewPraise != null){
            throw new APIException(ResultCodeEnum.ALREADY_EXIST);
        }
        return userPraiseMapper.insert(new UserPraise(JwtTokenUtils.getUserInfoA().getUserId(),sourceId, Objects.requireNonNull(SourceTypeEnum.getSourceType(sourceType)))) > 0;
    }

    @Override
    public Boolean cancelPraise(Integer sourceId, Integer sourceType) {
        LambdaQueryWrapper<UserPraise> eq = new QueryWrapper<UserPraise>().lambda()
                .eq(UserPraise::getSourceId, sourceId)
                .eq(UserPraise::getSourceType,sourceType)
                .eq(UserPraise::getUserId, JwtTokenUtils.getUserInfoA().getUserId());
        // 实现取消点赞逻辑
        UserPraise userNewPraise = userPraiseMapper.selectOne(eq);
        if (userNewPraise == null){
            throw new APIException(ResultCodeEnum.ALREADY_EXIST);
        }

        return userPraiseMapper.delete(eq) > 0;
    }

    @Override
    public Boolean commentGameNews(UserCommentParam param) {
        LoginUserVo userInfo = JwtTokenUtils.getUserInfoA();
        if (param.getCommentId() != null){
            UserComment userComment = UserCommentMapper.selectById(param.getCommentId());
            if (userComment == null){
                throw new APIException(ResultCodeEnum.COMMENT_DATA_NOT_EXIST);
            }
        }
        UserComment UserComment = new UserComment(param.getCommentId(), param.getSourceId(), userInfo.getUserId(), param.getContent(),SourceTypeEnum.getSourceType(param.getSourceType()));
        return UserCommentMapper.insert(UserComment) > 0;
    }

    @Override
    public Boolean deleteCommentGameNews(Integer commentId) {
        return null;
    }
}
